/*
 * @lc app=leetcode.cn id=40 lang=cpp
 *
 * [40] 组合总和 II
 *
 * https://leetcode-cn.com/problems/combination-sum-ii/description/
 *
 * algorithms
 * Medium (60.78%)
 * Likes:    939
 * Dislikes: 0
 * Total Accepted:    284.6K
 * Total Submissions: 468.3K
 * Testcase Example:  '[10,1,2,7,6,1,5]\n8'
 *
 * 给定一个候选人编号的集合 candidates 和一个目标数 target ，找出 candidates 中所有可以使数字和为 target 的组合。
 * 
 * candidates 中的每个数字在每个组合中只能使用 一次 。
 * 
 * 注意：解集不能包含重复的组合。 
 * 
 * 
 * 
 * 示例 1:
 * 
 * 
 * 输入: candidates = [10,1,2,7,6,1,5], target = 8,
 * 输出:
 * [
 * [1,1,6],
 * [1,2,5],
 * [1,7],
 * [2,6]
 * ]
 * 
 * 示例 2:
 * 
 * 
 * 输入: candidates = [2,5,2,1,2], target = 5,
 * 输出:
 * [
 * [1,2,2],
 * [5]
 * ]
 * 
 * 
 * 
 * 提示:
 * 
 * 
 * 1 <= candidates.length <= 100
 * 1 <= candidates[i] <= 50
 * 1 <= target <= 30
 * 
 * 
 */

// @lc code=start
class Solution {
// // 用used数组去重
// private: 
//     vector<int> path;
//     vector<vector<int>> res;
//     void backtracking(vector<int>& candidates, int target, int sum, int startIndex, vector<bool>& used) {
//         if(sum > target) return;
//         if(sum == target) {
//             res.push_back(path);
//             return;
//         }

//         for(int i = startIndex; i < candidates.size() && target >= sum + candidates[i]; i++) {
//             // 使用used数组进行去重操作
//             // used[i - 1] == true，说明同一树枝candidates[i - 1]使用过
//             // used[i - 1] == false，说明同一树层candidates[i - 1]使用过
//             // 要对同一树层使用过的元素进行跳过
//             if(i > startIndex && candidates[i] == candidates[i-1] && used[i] == false) continue; 
//             used[i] == true;  
//             path.push_back(candidates[i]);
//             backtracking(candidates, target, sum + candidates[i], i + 1, used);
//             path.pop_back();
//             used[i] == false;
//         }
//     }
// public:
//     vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
//         path.clear();
//         res.clear();
//         vector<bool> used(candidates.size(), false);
//         sort(candidates.begin(), candidates.end());
//         backtracking(candidates, target, 0, 0, used);
//         return res;
//     }


// // 用startIndex去重
// private: 
//     vector<int> path;
//     vector<vector<int>> res;
//     void backtracking(vector<int>& candidates, int target, int sum, int startIndex) {
//         if(sum > target) return;
//         if(sum == target) {
//             res.push_back(path);
//             return;
//         }

//         for(int i = startIndex; i < candidates.size() && target >= sum + candidates[i]; i++) {
//             // 仅适用startIndex进行去重操作。
//             if(i > startIndex && candidates[i] == candidates[i-1]) continue;   
//             path.push_back(candidates[i]);
//             backtracking(candidates, target, sum + candidates[i], i + 1);
//             path.pop_back();
//         }
//     }
// public:
//     vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
//         path.clear();
//         res.clear();
//         sort(candidates.begin(), candidates.end());
//         backtracking(candidates, target, 0, 0);
//         return res;
//     }


// // 复盘-startIndex去重
// private:
//     vector<int> path;
//     vector<vector<int>> res;
//     void backtracking(vector<int>& candidates, int target, int sum, int startIndex) {
//         if(sum > target) return;
//         if(sum == target) {
//             res.push_back(path);
//             return;
//         }

//         for(int i = startIndex; i < candidates.size() && target >= sum + candidates[i]; i++) {
//             if(i > startIndex && candidates[i] == candidates[i-1]) continue;

//             path.push_back(candidates[i]);
//             backtracking(candidates, target, sum + candidates[i], i + 1);
//             path.pop_back();
//         }
//     }

// public:
//     vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
//         path.clear();
//         res.clear();
//         sort(candidates.begin(), candidates.end());
//         backtracking(candidates, target, 0, 0);
//         return res;
//     }


// 复盘-used数组去重
private:
    vector<int> path;
    vector<vector<int>> res;
    void backtracking(vector<int>& candidates, int target, int sum, int startIndex, vector<bool>& used) {
        if(sum > target) return;
        if(sum == target) {
            res.push_back(path);
            return;
        }

        for(int i = startIndex; i < candidates.size() && target >= sum + candidates[i]; i++) {
            if(i > startIndex && candidates[i] == candidates[i-1] && used[i] == false) continue;

            used[i] = true;
            path.push_back(candidates[i]);
            backtracking(candidates, target, sum + candidates[i], i + 1, used);
            path.pop_back();
            used[i] = false;
        }
    }

public:
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        path.clear();
        res.clear();
        sort(candidates.begin(), candidates.end());
        vector<bool> used(candidates.size(), false);
        backtracking(candidates, target, 0, 0, used);
        return res;
    }


};
// @lc code=end

